#include "SConsoleStats.h"

const char* CONSOLE_FLAG_CLOSE_SERVE = "close\0";
const char* CONSOLE_FLAG_HELP = "?\0";
const char* CONSOLE_FLAG_USER = "user\0";
const char* CONSOLE_FLAG_BYE = "bye\0";
const char* CONSOLE_FLAG_SHUTDOWN = "shutdown\0";
const char* CONSOLE_FLAG_START_SERVICE = "start\0";

void CConsoleStatsConstructor(CConsoleStats* stats)
{
    //初始化变量
    stats->Destructor = CConsoleStatsDestructor;

    stats->MutexInit = ConsoleStatsMutexInit;

    stats->MutexLock  = ConsoleStatsLock;

    stats->MutexUnlock  = ConsoleStatsUnlock;

    stats->ConsoleMain =  ConsoleStatsMain;

    stats->isWorking = ConsoleStatsIsWorking;

    stats->isStarting = ConsoleStatsIsStarting;

    stats->TranslateMessage = ConsoleStatsTransport;

    stats->DispatchMessage = ConsoleStatsDispatch;

    stats->DoConsoleCloseServe = ConsoleStatsDoConsoleCloseServe;

    stats->DoConsoleStartServe = ConsoleStatsDoConsoleStartServe;

    stats->DoConsoleShutdown  = ConsoleStatsDoConsoleShutdown;

    stats->DoConsoleHelp = ConsoleStatsDoConsoleHelp;

    stats->DoConsoleBye = ConsoleStatsDoConsoleBye;

    stats->DoConsoleUser = ConsoleStatsDoConsoleUser;

    stats->mServeStats.IsWorking = 1;

    stats->mServeStats.IsStart = 0;

    stats->mServeStats.port = 0;

    stats->mServeStats.qlen = 0;

    stats->mastersocket = -1;

    memset(stats->mServeStats.servename,0,sizeof(stats->mServeStats.servename));

}

void CConsoleStatsDestructor(CConsoleStats* stats)
{
    if(stats)
        free(stats);
}

CConsoleStats* CConsoleStatsGetInstance()
{
    if(!StaticConsoleStats)
    {
        StaticConsoleStats = (CConsoleStats*)malloc(sizeof(CConsoleStats));
        StaticConsoleStats->Constructor = CConsoleStatsConstructor;
        //CConsoleStatsConstructor(StaticConsoleStats);
        StaticConsoleStats->Constructor (StaticConsoleStats);
    }
    return StaticConsoleStats;
}

int ConsoleStatsMutexInit(struct SConsoleStats* stats)
{
    return pthread_mutex_init(&stats->pmtforstats,0);
}

int ConsoleStatsLock(struct SConsoleStats* stats)
{
    return pthread_mutex_lock(&stats->pmtforstats);
}


int ConsoleStatsUnlock(struct SConsoleStats *  stats)
{
    return  pthread_mutex_unlock(&stats->pmtforstats);
}


int ConsoleStatsMain(struct SConsoleStats *  stats)
{
    if(!stats)
        stats = CConsoleStatsGetInstance();
    while(stats->isWorking(stats))
    {
        (void)sleep(INTERVAL_TIME);//等待时间


        stats->DispatchMessage(stats);

    }

    return 0;
}


//获取并解析命令
int ConsoleStatsTransport(struct SConsoleStats *  stats)
{
    int lengthofstr  = 0;
    char* tmpBuf;
    char consolebuf[MAX_LENGTH_OF_CONSOLE];
    int i =0;//,cc=0;
    if(!stats)
        stats = CConsoleStatsGetInstance();
    while(stats->isWorking(stats))
    {
        printf("you can enter commond now:(input '?' for help)\n>>");
        stats->enumconsoleflags = ECF_NONE;
        fflush(stdin);
        if(fgets(stats->buf,sizeof(stats->buf),stdin))
        {
            stats->MutexLock(stats);
            stats->buf[LINELEN]='\0';
            lengthofstr = strlen(stats->buf);
            if(lengthofstr>1)
            {
                tmpBuf = stats->buf;
                i=0;
                tmpBuf = gotonotblank(tmpBuf);
                i = getposofnotcharorc(tmpBuf,'?');
                strncpy(consolebuf,tmpBuf,i);
                consolebuf[i] = '\0';

                if(0==strcmp(consolebuf,CONSOLE_FLAG_CLOSE_SERVE))//关闭服务
                    stats->enumconsoleflags = ECF_CLOSE_SERVE;
                else       if(0==strcmp(consolebuf,CONSOLE_FLAG_HELP))//帮助
                    stats->enumconsoleflags = ECF_HELP;
                else     if(0==strcmp(consolebuf,CONSOLE_FLAG_USER))//登录
                    stats->enumconsoleflags = ECF_USER;
                else if(0==strcmp(consolebuf,CONSOLE_FLAG_BYE))//注销
                    stats->enumconsoleflags = ECF_BYE;
                else if(0==strcmp(consolebuf,CONSOLE_FLAG_SHUTDOWN))//关闭服务器
                    stats->enumconsoleflags = ECF_SHUT_DOWN;
                else if(0==strcmp(consolebuf,CONSOLE_FLAG_START_SERVICE))//开启服务
                    stats->enumconsoleflags = ECF_START_SERVE;
                else
                    stats->enumconsoleflags = ECF_NONE;
              //   printf("console:%s,%d\n",consolebuf,stats->enumconsoleflags);
            }
            else
                stats->enumconsoleflags = ECF_NONE;
            stats->MutexUnlock(stats);
        }
        else
        {
            stats->MutexLock(stats);
            stats->enumconsoleflags = ECF_NONE;
            stats->MutexUnlock(stats);
        }


        printf("cmd flushing..please wait..\n");
        (void)sleep(INTERVAL_TIME);//等待时间
    }
    return 1;
}

//执行命令
int ConsoleStatsDispatch(struct SConsoleStats *  stats)
{
    int tag =1;
    stats->MutexLock(stats);
    E_CONSOLE_FLAGS emctmp = stats->enumconsoleflags;
    stats->enumconsoleflags = ECF_NONE;
    stats->MutexUnlock(stats);
    switch(emctmp)
    {
    case ECF_CLOSE_SERVE:
        stats->DoConsoleCloseServe(stats);
        break;
    case ECF_START_SERVE:
        stats->DoConsoleStartServe(stats);
        break;
    case ECF_SHUT_DOWN:
        stats->DoConsoleShutdown(stats);
        break;
    case ECF_HELP:
        stats->DoConsoleHelp(stats);
        break;
    default:
        tag = 0;
        break;
    }
    return tag;
}


//关闭服务
int  ConsoleStatsDoConsoleCloseServe(struct SConsoleStats* stats)
{
    if(stats->mServeStats.IsStart)
    {
        stats->MutexLock(stats);
        if(stats->mastersocket>0)
            close(stats->mastersocket);
        stats->mastersocket = -1;
        stats->MutexUnlock(stats);
        stats->mServeStats.IsStart = 0;
        printf("Serve services close succeed.\n");
    }
    else
        printf("Serve services have closed.\n");

    return 0;
}

//开启服务
int ConsoleStatsDoConsoleStartServe(struct SConsoleStats* stats)
{
    if( !(stats->mServeStats.IsStart))
   {
      if(stats->mastersocket>0)
        printf("error?Serve services have start.\n");
        else
        {
            stats->mastersocket = socketInitTCP(stats->mServeStats.servename,stats->mServeStats.qlen,stats->mServeStats.port);
            if(stats->mastersocket>0)
            {
                stats->mServeStats.IsStart = 1;
                printf("Serve services start succeed.\n");
            }
        }
   }
   else
        printf("Serve services have start.\n");
   return 0;
}

//关闭服务器
int ConsoleStatsDoConsoleShutdown(struct SConsoleStats* stats)
{
    stats->MutexLock(stats);
    stats->mServeStats.IsWorking = 0;
    if(stats->mastersocket>0)
        close(stats->mastersocket);
    stats->mastersocket = -1;
    stats->MutexUnlock(stats);
    errexit("now exit.\n");
    return stats->mServeStats.IsWorking;
}

int ConsoleStatsDoConsoleUser(struct SConsoleStats* stats)
{

    return 0;
}

int ConsoleStatsDoConsoleBye(struct SConsoleStats* stats)
{


    return 0;
}

int  ConsoleStatsDoConsoleHelp(struct SConsoleStats* stats)
{
    printf("\nHelp:\n\tstart\tstart serve services.\n\tclose\tclose serve services.\n\tshutdown\tshutdown serve.\nother:\n\tuser\tbye\n");
    return 1;
}
/**辅助函数*/
int ConsoleStatsIsWorking(struct SConsoleStats *  stats)
{
    return stats->mServeStats.IsWorking;
}

int ConsoleStatsIsStarting(struct SConsoleStats *  stats)
{
    return stats->mServeStats.IsStart;
}
